<?php
namespace app\admin\controller\wxapp;
use app\admin\controller\Controller;
use app\admin\model\WxappReg as WxappRegModel;

/**
 * 商户申请认证记录
 */
class Apply extends Controller
{
    /**
     * 获取所有列表
     */
    public function index()
    {
        $model = new WxappRegModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }
	/**
     * 获取详情
     */
    public function detail($wxapp_reg_id)
    {
		//测试判断
		if($this->admin['user']['config_id']==0){
			$this->error('测试用户不可操作');
		}
        $model = WxappRegModel::detail(['wxapp_reg_id' => $wxapp_reg_id]);
		
        if (!$this->request->isAjax()) {
            return $this->fetch('detail', compact('model'));
        }
		$data = $this->postData('authentication');
		if($data['status']==0){
			return $this->renderSuccess('正在跳转', url('wxapp.apply/index'));
		}
		if($data['status']==40 AND empty($data['reject'])){
			return $this->renderError('请输入驳回原因');
		}
        // 更新记录
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功', url('wxapp.apply/index'));
        }
        $error = $model->getError() ?: '更新失败';
        return $this->renderError($error);
    }
	
	/**
     * 删除
     */
    public function delete($wxapp_reg_id)
    {
		//测试判断
		if($this->admin['user']['config_id']==0){
			return $this->renderError('测试用户不可操作');
		}
		$model = WxappRegModel::detail(['wxapp_reg_id' => $wxapp_reg_id]);
        if (!$model->remove()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
	
	public function search($wxapp_reg_id){
		$model = WxappRegModel::get($wxapp_reg_id);
		$msg = $model->search();
        echo $msg;
	}
}
